package com.jianyun.jyzs.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.utils.DensityUtil;

import java.util.Locale;


/**
 * Created by weiqinxiao on 2017/2/15.
 */

public class OptionMenu extends PopupWindow {

    //locale  english.
    private final static String LOCALE_EN = "en";

    //layout width when locale is en.
    private final static int LAYOUT_WIDTH_EN = 160;

    private View.OnClickListener onItemClickListener;
    private LinearLayout layoutCreateGroup;
    private LinearLayout layoutAddFriend;
    private LinearLayout layoutScanning;
    private LinearLayout layoutPin;
    private final TextView contentTextView;
    private final ImageView leftImageView;

    public OptionMenu(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content = inflater.inflate(R.layout.rce_pop_menu, null);
        setContentView(content);
        setWidth(LayoutParams.WRAP_CONTENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        layoutCreateGroup = (LinearLayout) content.findViewById(R.id.menuItemCreateGroup);
        layoutCreateGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) onItemClickListener.onClick(v);
            }
        });
        contentTextView = (TextView) content.findViewById(R.id.tv_right_text);
        leftImageView = (ImageView) content.findViewById(R.id.iv_left_prompt_icon);
        layoutAddFriend = (LinearLayout) content.findViewById(R.id.menuItemAddFriend);
        layoutAddFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) onItemClickListener.onClick(v);
            }
        });

        layoutScanning = (LinearLayout) content.findViewById(R.id.menuItemStartScanning);
        layoutScanning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) onItemClickListener.onClick(v);
            }
        });

        layoutPin = (LinearLayout) content.findViewById(R.id.menuItemCreatePIN);
        layoutPin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) onItemClickListener.onClick(v);

            }
        });
        //when locale is en,  enlarge the width of the layout.
        Locale locale = Locale.getDefault();

        if (locale.getLanguage().equalsIgnoreCase(LOCALE_EN)) {
            LayoutParams params = layoutCreateGroup.getLayoutParams();
            params.width = DensityUtil.dip2px(context,LAYOUT_WIDTH_EN);
            layoutCreateGroup.setLayoutParams(params);
            layoutAddFriend.setLayoutParams(params);
            layoutScanning.setLayoutParams(params);
        }

        setBackgroundDrawable(context.getResources().getDrawable(R.color.color_transparent));
        setOutsideTouchable(true);
        setFocusable(true);
    }

    public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void configPinDeleteMenu() {
        layoutAddFriend.setVisibility(View.GONE);
        layoutScanning.setVisibility(View.GONE);
        layoutPin.setVisibility(View.GONE);
        contentTextView.setText(R.string.rce_pin_delete_action);
        leftImageView.setImageResource(R.drawable.pin_delete_icon);
    }

}
